import { _decorator, Component, Button } from 'cc';
const { ccclass, property } = _decorator

import GameStorage from "../Data/GameStorage";
const KeyForSaving = 'sidebar_reward_data'

// /** * 侧边栏*/
@ccclass('CeBianLanPanel')
export default class CeBianLanPanel extends Component {
    public static Instance: CeBianLanPanel;
    @property(Button)
    close: Button | null = null
    @property(Button)
    Button_进入侧边栏: Button | null = null
    @property(Button)
    Button_领取奖励: Button | null = null
    @property(Button)
    Button_已领取奖励: Button | null = null
    rewardData: any
    protected async onLoad() {
        CeBianLanPanel.Instance = this;

        // this.ShowOff();

        //        // this.rewardData = GameDataMgr.Instance.load<RewardData>(KeyForSaving, RewardData) ?? new RewardData()
    }

    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }
    AddListener() {
        this.close.node.on('click', this.on_close, this)
        this.Button_进入侧边栏.node.on('click', this.on_Button_进入侧边栏, this)
        this.Button_领取奖励.node.on('click', this.on_Button_领取奖励, this)
        this.Button_已领取奖励.node.on('click', this.on_Button_已领取奖励, this)
    }
    RemoveListener() {
        this.close.node.off('click', this.on_close, this)
        this.Button_进入侧边栏.node.off('click', this.on_Button_进入侧边栏, this)
        this.Button_领取奖励.node.off('click', this.on_Button_领取奖励, this)
        this.Button_已领取奖励.node.off('click', this.on_Button_已领取奖励, this)
    }
    async Refresh() {

    }
    protected start(): void {
        this.rewardData = + (localStorage.getItem(this.storageKey) ?? '0');
        // this.btnGift.node.active = Date.now() - lastGet > 7 * 24 * 60 * 60 * 1000;  // 一周能领一次

        // const rewarded = 24 * 60 * 60 * 1000 > Date.now() - this.rewardData.lastRewardTime
        const rewarded = 100 * 1000 > Date.now() - this.rewardData.lastRewardTime
        const { scene } = tt.getLaunchOptionsSync();
        this.Button_进入侧边栏.node.active = !rewarded && scene != '021036';
        this.Button_领取奖励.node.active = !rewarded && scene == '021036';
        this.Button_已领取奖励.node.active = rewarded

        tt.onShow(res => {
            console.log("res", res);

            this.Button_进入侧边栏.node.active = !rewarded && res.scene != '021036';
            this.Button_领取奖励.node.active = !rewarded && res.scene == '021036';
            this.Button_已领取奖励.node.active = rewarded
        });

        // tt.onShow(res => {
        //     if (this.btnNavToScene)
        //         this.btnNavToScene.node.active = res.scene != '021036';
        //     if (this.btnGetGift)
        //         this.btnGetGift.node.active = res.scene == '021036';
        // });
    }
    on_close() {
        this.ShowOff()
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    async on_Button_进入侧边栏() {
        tt.navigateToScene({ scene: 'sidebar' })
    }
    storageKey = 'last_get_gift_time_2';
    async on_Button_领取奖励() {
        GameStorage.data.free_ad++;
        GameStorage.saveData();

        localStorage.setItem(this.storageKey, Date.now() + '');

        this.Button_进入侧边栏.node.active = false;
        this.Button_领取奖励.node.active = false;
        this.Button_已领取奖励.node.active = true;

        // PlayerStats.Instance.currencyAdd(50000)
        // this.rewardData.lastRewardTime = Date.now()
        // GameDataMgr.Instance.save<RewardData>(KeyForSaving, this.rewardData)

        // PlayerStats.Instance.currencyAdd(50000)
        // this.rewardData.lastRewardTime = Date.now()
        // GameDataMgr.Instance.save<RewardData>(KeyForSaving, this.rewardData)
    }
    async on_Button_已领取奖励() {
        this.ShowOff();
    }
}